#测试博客登录页面
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from common.Until import BlogDriver
class BlogLogin:
    url = ""
    driver = ""
    #构造函数
    def __init__(self):
        self.url = "http://127.0.0.1:8080/blog_login.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

    #成功登录的测试用例
    def LoginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#userName").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#userName").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        # time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(2)
        #找到博客首页用户的昵称，说明登录成功， 否则登录失败
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3").click()
        BlogDriver.getScreenShot()

        # self.driver.quit()
        #返回登录页面
        # self.driver.back()


    #异常登录测试用例
    def LoginFailTest(self):
        #若连续多次的send_keys则会出现关键词拼接，而不是替换
        #若要替换，需先选中元素clear一下
        self.driver.find_element(By.CSS_SELECTOR,"#userName").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()

        #添加正确的账号和错误的密码
        self.driver.find_element(By.CSS_SELECTOR,"#userName").send_keys("admin")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #检查是否登录失败
        #使用显示等待中的alert_is_present
        wait = WebDriverWait(self.driver,2)
        wait.until(EC.alert_is_present())
        alert = self.driver.switch_to.alert
        assert alert.text == "用户名或密码错误"
        alert.accept()
        # 添加屏幕截图，查看运行时是否出现问题
        BlogDriver.getScreenShot()

        #添加错误的账号和正确的密码
        self.driver.find_element(By.CSS_SELECTOR,"#userName").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#userName").send_keys("zs")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #注意显示等待只作用于当前那段代码
        wait = WebDriverWait(self.driver,2)
        wait.until(EC.alert_is_present())
        alert = self.driver.switch_to.alert

        #断言一下，检测是否符合预期
        assert alert.text == "用户名或密码错误"
        alert.accept()
        #添加屏幕截图，查看运行时是否出现问题
        # BlogDriver.getScreenShot()

        #添加错误的账号和错误的密码
        self.driver.find_element(By.CSS_SELECTOR,"#userName").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#userName").send_keys("zs")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()

        wait = WebDriverWait(self.driver,2)
        wait.until(EC.alert_is_present())
        assert alert.text == "用户名或密码错误"
        # alert.accept()#点击确认
        alert.dismiss()#点击取消
        # 添加屏幕截图，查看运行时是否出现问题
        BlogDriver.getScreenShot()

        # 添加非空账号和密码为空
        self.driver.find_element(By.CSS_SELECTOR, "#userName").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#userName").send_keys("zs")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        wait = WebDriverWait(self.driver, 2)
        wait.until(EC.alert_is_present())
        assert alert.text == "用户名或密码错误"
        alert.dismiss()  # 点击取消

        # 添加账号为空和密码非空
        self.driver.find_element(By.CSS_SELECTOR, "#userName").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        wait = WebDriverWait(self.driver, 2)
        wait.until(EC.alert_is_present())
        assert alert.text == "用户名或密码错误"
        alert.dismiss()#点击取消

        # 添加账号和密码都为空
        self.driver.find_element(By.CSS_SELECTOR, "#userName").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        wait = WebDriverWait(self.driver, 2)
        wait.until(EC.alert_is_present())
        assert alert.text == "用户名或密码错误"
        alert.dismiss()  # 点击取消
